package collection;

import java.util.ArrayList;
import java.util.Collection;

/**
 *  Collection接口遍历对象方式 for循环增强
 *  for(元素类型 元素名: 集合名或数组名){
 *      访问元素
 *  }
 */
public class CollectionFor {

    public static void main(String[] args) {

        /*
            增强 for 循环，可以代替 iterator 迭代器，特点：增强 for 就是简化版的 iterator
            本质一样。只能用于遍历集合或数组
         */
        Collection col = new ArrayList();
        col.add(new Test("张三", "aaa"));
        col.add(new Test("李四", "bbb"));

        for (Object o : col) {
            System.out.println(o);
        }
    }
}

class Test {

    private String name;

    private String author;

    public Test(String name, String author) {
        this.name = name;
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Test{" +
                "name='" + name + '\'' +
                ", author='" + author +
                '}';
    }
}